package server.business.poker.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;


public class PokerGenerator {
    /**
     * 所有牌
     */
    private final Integer allPai[]={
            1,2,3,4,5,6,7,8,9,10,11,12,13,
            14,15,16,17,18,19,20,21,22,23,24,25,26,
            27,28,29,30,31,32,33,34,35,36,37,38,39,
            40,41,42,43,44,45,46,47,48,49,50,51,52
    };
    
    private final Integer noHua[]={
        1,14,27,40,
        2,15,28,41,
        3,16,29,42,
        4,17,30,43,
        5,18,31,44,
        6,19,32,45,
        7,20,33,46,
        8,21,34,47,
        9,22,35,48,
        10,23,36,49
    };
    
    
    private final List<Integer> allList = new ArrayList<Integer>(Arrays.asList(allPai));
    
    /**
     * 所有1
     */
    private static final Integer one[]={
        1,14,27,40
    };
    private List<Integer>oneList;
    /**
     * 所有2
     */
    private static final Integer two[]={
        2,15,28,41
    };
    private List<Integer>twoList;
    
    /**
     * 所有3
     */
    private static final Integer three[]={
        3,16,29,42
    };
    private List<Integer>threeList;
    /**
     * 所有4
     */
    private static final Integer four[]={
        4,17,30,43
    };
    private List<Integer>fourList;
    /**
     * 所有5
     */
    private static final Integer five[]={
        5,18,31,44
    };
    private List<Integer>fiveList;
    /**
     * 所有6
     */
    private static final Integer six[]={
        6,19,32,45
    };
    private List<Integer>sixList;
    /**
     * 所有7
     */
    private static final Integer seven[]={
        7,20,33,46
    };
    private List<Integer>sevenList;
    /**
     * 所有8
     */
    private static final Integer eight[]={
        8,21,34,47
    };
    private List<Integer>eightList;
    /**
     * 所有9
     */
    private static final Integer nine[]={
        9,22,35,48
    };
    private List<Integer>nineList;
    /**
     * 所有10
     */
    private static final Integer ten[]={
        10,23,36,49
    };
    private List<Integer>tenList;
    /**
     * 所有J
     */
    private static final Integer J[]={
        11,24,37,50
    };
    /**
     * 所有Q
     */
    private static final Integer Q[]={
        12,25,38,51
    };
    
    /**
     * 所有K
     */
    private static final Integer K[]={
        13,26,39,52
    };
    /**
     * 
     */
    
    private static final Integer hua[] = {
            11,12,13,
            24,25,26,
            37,38,39,
            50,51,52
    };
    private List<Integer>huaList;
    
    /**
    
    private int xiaoXiaoNiu[] = {
      11112,11113,11114,11115,
      11122,11123,11124,11125,
      11222,11223,11224,
      12222,12223
    };
    **/
    
    /**
     * 小小牛
     * 
     */
    
    private static final String[]xiaoxiaoNiu = {
            "4_1&1_2","4_1&1_3","4_1&1_4","4_1&1_5",
            "3_1&2_2","3_1&1_2&1_3","3_1&1_2&1_4","3_1&1_2&1_5",
            "2_1&3_2","2_1&2_2&1_3","2_1&2_2&1_4",
            "1_1&4_2","1_1&3_2&1_3"
    };
    
    private static final String[] tenString_3 = {
      "2_1&1_8","1_1&1_2&1_7","1_1&1_3&1_6", "1_1&1_4&1_5",
      "2_2&1_6","1_2&1_3&1_5","1_2&2_4",
      "2_3&1_4","1_3&1_8&1_9",
      "1_4&1_7&1_9","1_4&2_8",
      "1_5&1_6&1_9",
      "2_6&1_8",
      "2_7&1_6",
      "2_9&1_2",
      "1_1&1_9&1_10","1_2&1_8&1_10","1_3&1_7&1_10","1_4&1_6&1_10","2_5&1_10"//两个数之和为10，第三个数为10
    };
    
    private static final String[] tenString_2 = {
        "1_1&1_9","1_2&1_8","1_3&1_7","1_4&1_6","2_5"
    };
    
    private PokerGenerator(){
        
        init();
    }
    
    public static synchronized PokerGenerator getInstance(){
        
        return new PokerGenerator();
    }
    
    private void init(){
        oneList = new ArrayList<Integer>(Arrays.asList(one));
        twoList = new ArrayList<Integer>(Arrays.asList(two));
        threeList = new ArrayList<Integer>(Arrays.asList(three));
        fourList = new ArrayList<Integer>(Arrays.asList(four));
        fiveList = new ArrayList<Integer>(Arrays.asList(five));
        sixList = new ArrayList<Integer>(Arrays.asList(six));
        sevenList = new ArrayList<Integer>(Arrays.asList(seven));
        eightList = new ArrayList<Integer>(Arrays.asList(eight));
        nineList = new ArrayList<Integer>(Arrays.asList(nine));
        tenList = new ArrayList<Integer>(Arrays.asList(ten));
        huaList = new ArrayList<Integer>(Arrays.asList(hua));
    }
    
    /**
     * 牛牛分三种情况：
     * 情况一，2个花，3个数字，其中3个数字之和要为10。
     * 情况二，1个花，1个十，3个数字之和要为10。
     * 情况三，5个数字。其中前3个数字之和要为10，后三个数字之和要为10
     * 
     * @return
     */
    public int[][]getNiuNiu(){
        int [][]result = new int[4][5];
        
        Random r = new Random();
        
        int qk = r.nextInt(3)+1;
        
        List <Integer> selectedList = new ArrayList<Integer>();
        
        switch(qk){
            case 1://情况一，2个花
            {
                selectedList.addAll(get3Number());
                
                List <Integer>hua2 = getRandom(huaList,2);
                
                selectedList.addAll(hua2);
                
            };break;
            
            case 2://情况二，一个花
            {
                selectedList.addAll(get3Number());
                List<Integer> ten_1 = getRandom(tenList,1);
                List<Integer> hua_1 = getRandom(huaList,1);
                selectedList.addAll(ten_1);
                selectedList.addAll(hua_1);
                
            };break;
            
            case 3://情况三，没有花
            {
                selectedList.addAll(get3Number());
                selectedList.addAll(get2Number());
                
            };break;
        }
        
        for(int i=0;i<5;i++){
            
            result[0][i] = selectedList.get(i);
        }
        
        List<Integer>rest15 = getRest15(selectedList);
        
        int k = 0;
        for(int i=1;i<4;i++){
            
            for(int j=0;j<5;j++){
                result[i][j] = rest15.get(k);
                k++;
            }
        }
        
        init();
        return result;
    }
    
    
    public static void main(String []args){

        //
    }
    
    /**
     * 得到炸弹
     * 
     * @return
     */
    public  int[][]getBomb(){
        
        int result[][] = new int[4][5]; 
        
        Random r = new Random();
        
        int index = r.nextInt(13) + 1;
        
        Integer four_[] = null;
        
        switch(index){
            case 1:
                four_ = one;break;
            case 2:
                four_ = two;break;
            case 3:
                four_ = three;break;
            case 4:
                four_ = four;break;
            case 5:
                four_ = five;break;
            case 6:
                four_ = six;break;
            case 7:
                four_ = seven;break;
            case 8:
                four_ = eight;break;
            case 9:
                four_ = nine;break;
            case 10:
                four_ = ten;break;
            case 11:
                four_ = J;break;
            case 12:
                four_ = Q;break;
            case 13:
                four_ = K;break;
                
            default:break;
        }
        
        List<Integer> rest16 = getRest16(Arrays.asList(four_));
        
        for(int i=0;i<4;i++){
            
            result[0][i] = four_[i];
        }
        
        result[0][4] = rest16.get(0);
        
        
        //remove 11112,11113,11114,11115,11116
        int total = 0;
        
        for(int i=0;i<5;i++){
            
            int value = result[0][i]%13;
            
            if(value == 0){
                
                value = 10;
            }
            total = total + value;
        }
        
        if(total <=10){
            
            return getBomb();
        }
        
        int m = 1;
        
        for(int i=1;i<4;i++){
            
            for(int j=0;j<5;j++){
                
                result[i][j] = rest16.get(m);
                
                m++;
            }
        }
        
        init();
        return result;
        
    }
    
    private List<Integer> getNewArrayList(List<Integer>all,List<Integer>selected){
        
        List<Integer> newList = new ArrayList<Integer>();
        
        for(Integer a : all){
            
            if(selected.contains(a)){
                
                continue;
            }else{
                
                newList.add(a);
            }
        }
        return newList;
    }
    
    /**
    private static Integer[] getRandom(Integer seed[],int length){
     // 种子你可以随意生成，但不能重复
        //int[] seed = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        Integer[] ranArr = new Integer[length];
        Random ran = new Random();
        // 数量你可以自己定义。
        for (int i = 0; i < length; i++) {
          // 得到一个位置
          int j = ran.nextInt(seed.length - i);
          // 得到那个位置的数值
          ranArr[i] = seed[j];
          // 将最后一个未用的数字放到这里
          seed[j] = seed[seed.length - 1 - i];
        }
        
        return ranArr;
        
    }
    **/
    
    
    private List<Integer> getRandom(List<Integer>seed,int length){
        
        List <Integer> selectedList = null;
        
        Integer [] selfSeed = new Integer[seed.size()];
        
        for(int i=0;i<seed.size();i++){
            
            selfSeed[i] = seed.get(i);
        }
        
        Integer[] ranArr = new Integer[length];
        
        Random ran = new Random();
        // 数量你可以自己定义。
        for (int i = 0; i < length; i++) {
          // 得到一个位置
          int j = ran.nextInt(selfSeed.length - i);
          // 得到那个位置的数值
          ranArr[i] = selfSeed[j];
          // 将最后一个未用的数字放到这里
          selfSeed[j] = selfSeed[selfSeed.length - 1 - i];
        }
        selectedList = new ArrayList<Integer>(Arrays.asList(ranArr));
        //selectedList.addAll();
        
        seed.removeAll(selectedList);
        
        return selectedList;
    }
    
    public int[][] getWuHua(){
        
        int [][]result = new int[4][5];
        
        List<Integer>selected = getRandom(huaList,5);
        
        List<Integer> rest = getRest15(selected);
        
        for(int i=0;i<5;i++){
            
            result[0][i] = selected.get(i);
        }
        
        int k=0;
        
        for (int i=1;i<4;i++){
            
            for(int j=0;j<5;j++){
                
                result[i][j] = rest.get(k);
                
                k++;
            }
        }
        
        init();
        return result;
    }
    
    
    /**
     * 得到小小牛以及其它牌
     * 
     * 第一行代表小小牛，后面的牌随机生成
     * 
     * @return
     */
    public int[][]getXiaoXiaoNiux(){
        
        int [][]result = new int[4][5];
        
        Random r = new Random();
        
        int index = r.nextInt(xiaoxiaoNiu.length);
        
        String str = xiaoxiaoNiu[index];
        
        String arr[]= str.split("&");
        
        List<Integer> list = new ArrayList<Integer>();
        
        for(String s:arr){
            
            String detail [] = s.split("_");
            
            int n = Integer.parseInt(detail[0]);
            
            int num = Integer.parseInt(detail[1]);
            
            switch(num){
                case 1:
                    
                    list.addAll(getRandom(oneList,n));break;
                case 2:
                    list.addAll(getRandom(twoList,n));break;
                case 3:
                    list.addAll(getRandom(threeList,n));break;
                case 4:
                    list.addAll(getRandom(fourList,n));break;
                case 5:
                    list.addAll(getRandom(fiveList,n));break;
                case 6:
                    list.addAll(getRandom(sixList,n));break;
                    default:break;
            }
            
        }
        
        for(int i=0;i<5;i++){
            
            result[0][i] = list.get(i);
        }
        
        List<Integer> rest = getRest15(list);
        
        int k=0;
        
        for(int i=1;i<4;i++){
            
            for(int j=0;j<5;j++){
                
                result[i][j] = rest.get(k);
                
                k++;
            }
        }
        
        init();
        return result;
    }
    
    private List<Integer> getRest15(List<Integer>selected){
        
        List<Integer> newList = getNewArrayList(allList,selected);
        
        //Integer []newSeed = (Integer[])newList.toArray(new Integer[newList.size()]);
        
        List<Integer> rest = getRandom(newList,15);
        
        return rest;
        
    }
    
    private List<Integer> getRest16(List<Integer>selected){
        
        List<Integer> newList = getNewArrayList(allList,selected);
        
        //Integer []newSeed = (Integer[])newList.toArray(new Integer[newList.size()]);
        
        List<Integer> rest = getRandom(newList,16);
        
        return rest;
        
    }
    
    private List<Integer> get3Number(){
       List <Integer> selectedList = new ArrayList<Integer>();
        
        Random r = new Random();
        
        int threeIndex =r.nextInt(tenString_3.length);
        
        String threeStr = tenString_3[threeIndex];
        
        String str[] = threeStr.split("&");
        
        for(String s:str){
            
            String ss[] = s.split("_");
            
            int n = Integer.parseInt(ss[0]);
            
            int num = Integer.parseInt(ss[1]);
            
            List<Integer> is = null;
            
            switch(num){
                case 1:
                   is = getRandom(oneList,n);break;
                case 2:
                    is = getRandom(twoList,n);break;
                case 3:
                    is = getRandom(threeList,n);break;
                case 4:
                    is = getRandom(fourList,n);break;
                case 5:
                    is = getRandom(fiveList,n);break;
                case 6:
                    is = getRandom(sixList,n);break;
                case 7:
                    is = getRandom(sevenList,n);break;
                case 8:
                    is = getRandom(eightList,n);break;
                case 9:
                    is = getRandom(nineList,n);break;
                case 10:
                    is = getRandom(tenList,n);break;
                    default:break;
            }
            selectedList.addAll(is);
        }
        
        return selectedList;
        
    }
    
    
    private List<Integer> get2Number(){
        List <Integer> selectedList = new ArrayList<Integer>();
         
         Random r = new Random();
         
         int threeIndex =r.nextInt(tenString_2.length);
         
         String threeStr = tenString_2[threeIndex];
         
         String str[] = threeStr.split("&");
         
         for(String s:str){
             
             String ss[] = s.split("_");
             
             int n = Integer.parseInt(ss[0]);
             
             int num = Integer.parseInt(ss[1]);
             
             List<Integer> is = null;
             
             switch(num){
                 case 1:
                    is = getRandom(oneList,n);break;
                 case 2:
                     is = getRandom(twoList,n);break;
                 case 3:
                     is = getRandom(threeList,n);break;
                 case 4:
                     is = getRandom(fourList,n);break;
                 case 5:
                     is = getRandom(fiveList,n);break;
                 case 6:
                     is = getRandom(sixList,n);break;
                 case 7:
                     is = getRandom(sevenList,n);break;
                 case 8:
                     is = getRandom(eightList,n);break;
                 case 9:
                     is = getRandom(nineList,n);break;
                     
                     default:break;
             }
             selectedList.addAll(is);
         }
         
         return selectedList;
         
     }
    
    public int[][]getCommonPokers(){
        
        int result[][]= new int[4][5];
        
        List<Integer> list = getRandom(allList,20);
        
        int m = 0;
        
        for(int i=0;i<4;i++){
            
            for(int j=0;j<5;j++){
                
                result[i][j] = list.get(m);
                
                m++;
            }
        }
        
        init();
        return result;
    }
    
}
